package com.jjrwa.utils;

import com.intellij.psi.PsiType;

import javax.swing.*;
import java.util.Random;

public abstract class CommonUtils {

    public final static ImageIcon GRBC_ICON = new ImageIcon("/src/main/resources/awy.png");

    /**
     * 去掉下划线 且 设置第一个字母为大小或小写
     *
     * @param value
     * @param upperFrist
     * @return
     */
    public static String clearLine(String value, boolean upperFrist) {
        value = value.toLowerCase();
        StringBuffer sub = new StringBuffer();
        for (char c : value.toCharArray()) {
            if (upperFrist) {
                sub.append(String.valueOf(c).toUpperCase());
                upperFrist = false;
                continue;
            }
            if (c == '_') {
                upperFrist = true;
                continue;
            }
            sub.append(c);
        }
        return sub.toString();
    }

    public static boolean needImport(PsiType psiType) {
        if (psiType == null) {
            return false;
        }
        if (TypeMapping.basicJavaType(psiType.getCanonicalText())) {
            return false;
        }
        if (psiType.getInternalCanonicalText().startsWith("java.lang")) {
            return false;
        }
        return true;
    }

    /**
     * 添加下划线
     *
     * @param value
     * @return
     */
    public static String addLine(String value) {
        StringBuffer sub = null;
        for (char c : value.toCharArray()) {
            if (sub == null) {
                sub = new StringBuffer();
                sub.append(c);
                continue;
            }
            if (Character.isUpperCase(c)) {
                sub.append('_');
            }
            sub.append(c);
        }
        return sub.toString().toLowerCase();
    }

    public static String firstToUpperCase(String value) {
        if (isBlank(value)) {
            return "";
        }
        return new StringBuilder()
                .append(Character.toUpperCase(value.charAt(0))).
                        append(value.substring(1))
                .toString();
    }

    public static String firstLowerCase(String value) {
        if (isBlank(value)) {
            return "";
        }
        return new StringBuilder()
                .append(Character.toLowerCase(value.charAt(0))).
                        append(value.substring(1))
                .toString();
    }

    public static String buildMappingValue(String value) {
        if (isBlank(value)) {
            return "";
        }
        StringBuilder sub = new StringBuilder();
        for (char c : value.toCharArray()) {
            if (Character.isUpperCase(c)) {
                sub.append("/");
                c = Character.toLowerCase(c);
            }
            sub.append(c);
        }
        return sub.toString();
    }


    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String buildSerialVersionUID() {
        Random r = new Random();
        //第一位
        int firstValue = r.nextInt(8) + 1;
        StringBuilder sub = new StringBuilder();
        sub.append(firstValue);
        int i = 1;
        while (i < 19) {
            sub.append(r.nextInt(9));
            i++;
        }
        sub.append("L");
        return sub.toString();
    }
}
